home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gxpath.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  23.5 KB  |  835 lines

  1. /* Copyright (C) 1989, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gxpath.c,v 1.2 2000/09/19 19:00:39 lpd Exp $ */
  20. /* Internal path management routines for Ghostscript library */
  21. #include "gx.h"
  22. #include "gserrors.h"
  23. #include "gsstruct.h"
  24. #include "gxfixed.h"
  25. #include "gzpath.h"
  26.  
  27. /* These routines all assume that all points are */
  28. /* already in device coordinates, and in fixed representation. */
  29. /* As usual, they return either 0 or a (negative) error code. */
  30.  
  31. /* Forward references */
  32. private int path_alloc_copy(P1(gx_path *));
  33. private int gx_path_new_subpath(P1(gx_path *));
  34.  
  35. #ifdef DEBUG
  36. private void gx_print_segment(P1(const segment *));
  37.  
  38. #  define trace_segment(msg, pseg)\
  39.      if ( gs_debug_c('P') ) dlprintf(msg), gx_print_segment(pseg);
  40. #else
  41. #  define trace_segment(msg, pseg) DO_NOTHING
  42. #endif
  43.  
  44. /* Check a point against a preset bounding box. */
  45. #define outside_bbox(ppath, px, py)\
  46.  (px < ppath->bbox.p.x || px > ppath->bbox.q.x ||\
  47.   py < ppath->bbox.p.y || py > ppath->bbox.q.y)
  48. #define check_in_bbox(ppath, px, py)\
  49.   if ( outside_bbox(ppath, px, py) )\
  50.     return_error(gs_error_rangecheck)
  51.  
  52. /* Structure descriptors for paths and path segment types. */
  53. public_st_path();
  54. private_st_path_segments();
  55. private_st_segment();
  56. private_st_line();
  57. private_st_line_close();
  58. private_st_curve();
  59. private_st_subpath();
  60.  
  61. /* ------ Initialize/free paths ------ */
  62.  
  63. private rc_free_proc(rc_free_path_segments);
  64. private rc_free_proc(rc_free_path_segments_local);
  65.  
  66. private void
  67. gx_path_init_contents(gx_path * ppath)
  68. {
  69.     ppath->box_last = 0;
  70.     ppath->first_subpath = ppath->current_subpath = 0;
  71.     ppath->subpath_count = 0;
  72.     ppath->curve_count = 0;
  73.     path_update_newpath(ppath);
  74.     ppath->bbox_set = 0;
  75. }
  76.  
  77. /*
  78.  * Initialize a path contained in an already-heap-allocated object,
  79.  * optionally allocating its segments.
  80.  */
  81. private int
  82. path_alloc_segments(gx_path_segments ** ppsegs, gs_memory_t * mem,
  83.             client_name_t cname)
  84. {
  85.     rc_alloc_struct_1(*ppsegs, gx_path_segments, &st_path_segments,
  86.               mem, return_error(gs_error_VMerror), cname);
  87.     (*ppsegs)->rc.free = rc_free_path_segments;
  88.     return 0;
  89. }
  90. int
  91. gx_path_init_contained_shared(gx_path * ppath, const gx_path * shared,
  92.                   gs_memory_t * mem, client_name_t cname)
  93. {
  94.     if (shared) {
  95.     if (shared->segments == &shared->local_segments) {
  96.         lprintf1("Attempt to share (local) segments of path 0x%lx!\n",
  97.              (ulong) shared);
  98.         return_error(gs_error_Fatal);
  99.     }
  100.     *ppath = *shared;
  101.     rc_increment(ppath->segments);
  102.     } else {
  103.     int code = path_alloc_segments(&ppath->segments, mem, cname);
  104.  
  105.     if (code < 0)
  106.         return code;
  107.     gx_path_init_contents(ppath);
  108.     }
  109.     ppath->memory = mem;
  110.     ppath->allocation = path_allocated_contained;
  111.     return 0;
  112. }
  113.  
  114. /*
  115.  * Allocate a path on the heap, and initialize it.  If shared is NULL,
  116.  * allocate a segments object; if shared is an existing path, share its
  117.  * segments.
  118.  */
  119. gx_path *
  120. gx_path_alloc_shared(const gx_path * shared, gs_memory_t * mem,
  121.              client_name_t cname)
  122. {
  123.     gx_path *ppath = gs_alloc_struct(mem, gx_path, &st_path, cname);
  124.  
  125.     if (ppath == 0)
  126.     return 0;
  127.     if (shared) {
  128.     if (shared->segments == &shared->local_segments) {
  129.         lprintf1("Attempt to share (local) segments of path 0x%lx!\n",
  130.              (ulong) shared);
  131.         gs_free_object(mem, ppath, cname);
  132.         return 0;
  133.     }
  134.     *ppath = *shared;
  135.     rc_increment(ppath->segments);
  136.     } else {
  137.     int code = path_alloc_segments(&ppath->segments, mem, cname);
  138.  
  139.     if (code < 0) {
  140.         gs_free_object(mem, ppath, cname);
  141.         return 0;
  142.     }
  143.     gx_path_init_contents(ppath);
  144.     }
  145.     ppath->memory = mem;
  146.     ppath->allocation = path_allocated_on_heap;
  147.     return ppath;
  148. }
  149.  
  150. /*
  151.  * Initialize a stack-allocated path.  This doesn't allocate anything,
  152.  * but may still share the segments.
  153.  */
  154. int
  155. gx_path_init_local_shared(gx_path * ppath, const gx_path * shared,
  156.               gs_memory_t * mem)
  157. {
  158.     if (shared) {
  159.     if (shared->segments == &shared->local_segments) {
  160.         lprintf1("Attempt to share (local) segments of path 0x%lx!\n",
  161.              (ulong) shared);
  162.         return_error(gs_error_Fatal);
  163.     }
  164.     *ppath = *shared;
  165.     rc_increment(ppath->segments);
  166.     } else {
  167.     rc_init_free(&ppath->local_segments, mem, 1,
  168.              rc_free_path_segments_local);
  169.     ppath->segments = &ppath->local_segments;
  170.     gx_path_init_contents(ppath);
  171.     }
  172.     ppath->memory = mem;
  173.     ppath->allocation = path_allocated_on_stack;
  174.     return 0;
  175. }
  176.  
  177. /*
  178.  * Ensure that a path owns its segments, by copying the segments if
  179.  * they currently have multiple references.
  180.  */
  181. int
  182. gx_path_unshare(gx_path * ppath)
  183. {
  184.     int code = 0;
  185.  
  186.     if (gx_path_is_shared(ppath))
  187.     code = path_alloc_copy(ppath);
  188.     return code;
  189. }
  190.  
  191. /*
  192.  * Free a path by releasing its segments if they have no more references.
  193.  * This also frees the path object iff it was allocated by gx_path_alloc.
  194.  */
  195. void
  196. gx_path_free(gx_path * ppath, client_name_t cname)
  197. {
  198.     rc_decrement(ppath->segments, cname);
  199.     /* Clean up pointers for GC. */
  200.     ppath->box_last = 0;
  201.     ppath->segments = 0;    /* Nota bene */
  202.     if (ppath->allocation == path_allocated_on_heap)
  203.     gs_free_object(ppath->memory, ppath, cname);
  204. }
  205.  
  206. /*
  207.  * Assign one path to another, adjusting reference counts appropriately.
  208.  * Note that this requires that segments of the two paths (but not the path
  209.  * objects themselves) were allocated with the same allocator.  Note also
  210.  * that since it does the equivalent of a gx_path_new(ppto), it may allocate
  211.  * a new segments object for ppto.
  212.  */
  213. int
  214. gx_path_assign_preserve(gx_path * ppto, gx_path * ppfrom)
  215. {
  216.     gx_path_segments *fromsegs = ppfrom->segments;
  217.     gx_path_segments *tosegs = ppto->segments;
  218.     gs_memory_t *mem = ppto->memory;
  219.     gx_path_allocation_t allocation = ppto->allocation;
  220.  
  221.     if (fromsegs == &ppfrom->local_segments) {
  222.     /* We can't use ppfrom's segments object. */
  223.     if (tosegs == &ppto->local_segments || gx_path_is_shared(ppto)) {
  224.         /* We can't use ppto's segments either.  Allocate a new one. */
  225.         int code = path_alloc_segments(&tosegs, ppto->memory,
  226.                        "gx_path_assign");
  227.  
  228.         if (code < 0)
  229.         return code;
  230.         rc_decrement(ppto->segments, "gx_path_assign");
  231.     } else {
  232.         /* Use ppto's segments object. */
  233.         rc_free_path_segments_local(tosegs->rc.memory, tosegs,
  234.                     "gx_path_assign");
  235.     }
  236.     tosegs->contents = fromsegs->contents;
  237.     ppfrom->segments = tosegs;
  238.     rc_increment(tosegs);    /* for reference from ppfrom */
  239.     } else {
  240.     /* We can use ppfrom's segments object. */
  241.     rc_increment(fromsegs);
  242.     rc_decrement(tosegs, "gx_path_assign");
  243.     }
  244.     *ppto = *ppfrom;
  245.     ppto->memory = mem;
  246.     ppto->allocation = allocation;
  247.     return 0;
  248. }
  249.  
  250. /*
  251.  * Assign one path to another and free the first path at the same time.
  252.  * (This may do less work than assign_preserve + free.)
  253.  */
  254. int
  255. gx_path_assign_free(gx_path * ppto, gx_path * ppfrom)
  256. {
  257.     /*
  258.      * Detect the special case where both paths have non-shared local
  259.      * segments, since we can avoid allocating new segments in this case.
  260.      */
  261.     if (ppto->segments == &ppto->local_segments &&
  262.     ppfrom->segments == &ppfrom->local_segments &&
  263.     !gx_path_is_shared(ppto)
  264.     ) {
  265. #define fromsegs (&ppfrom->local_segments)
  266. #define tosegs (&ppto->local_segments)
  267.     gs_memory_t *mem = ppto->memory;
  268.     gx_path_allocation_t allocation = ppto->allocation;
  269.  
  270.     rc_free_path_segments_local(tosegs->rc.memory, tosegs,
  271.                     "gx_path_assign_free");
  272.     /* We record a bogus reference to fromsegs, which */
  273.     /* gx_path_free will undo. */
  274.     *ppto = *ppfrom;
  275.     rc_increment(fromsegs);
  276.     ppto->segments = tosegs;
  277.     ppto->memory = mem;
  278.     ppto->allocation = allocation;
  279. #undef fromsegs
  280. #undef tosegs
  281.     } else {
  282.     /* In all other cases, just do assign + free. */
  283.     int code = gx_path_assign_preserve(ppto, ppfrom);
  284.  
  285.     if (code < 0)
  286.         return code;
  287.     }
  288.     gx_path_free(ppfrom, "gx_path_assign_free");
  289.     return 0;
  290. }
  291.  
  292. /*
  293.  * Free the segments of a path when their reference count goes to zero.
  294.  * We do this in reverse order so as to maximize LIFO allocator behavior.
  295.  * We don't have to worry about cleaning up pointers, because we're about
  296.  * to free the segments object.
  297.  */
  298. private void
  299. rc_free_path_segments_local(gs_memory_t * mem, void *vpsegs,
  300.                 client_name_t cname)
  301. {
  302.     gx_path_segments *psegs = (gx_path_segments *) vpsegs;
  303.     segment *pseg;
  304.  
  305.     if (psegs->contents.subpath_first == 0)
  306.     return;            /* empty path */
  307.     pseg = (segment *) psegs->contents.subpath_current->last;
  308.     while (pseg) {
  309.     segment *prev = pseg->prev;
  310.  
  311.     trace_segment("[P]release", pseg);
  312.     gs_free_object(mem, pseg, cname);
  313.     pseg = prev;
  314.     }
  315. }
  316. private void
  317. rc_free_path_segments(gs_memory_t * mem, void *vpsegs, client_name_t cname)
  318. {
  319.     rc_free_path_segments_local(mem, vpsegs, cname);
  320.     gs_free_object(mem, vpsegs, cname);
  321. }
  322.  
  323. /* ------ Incremental path building ------ */
  324.  
  325. /* Guarantee that a path's segments are not shared with any other path. */
  326. #define path_unshare(ppath)\
  327.   BEGIN\
  328.     if ( gx_path_is_shared(ppath) ) {\
  329.       int code_;\
  330.       if( (code_ = path_alloc_copy(ppath)) < 0 ) return code_;\
  331.     }\
  332.   END
  333.  
  334. /* Macro for opening the current subpath. */
  335. /* ppath points to the path; sets psub to ppath->current_subpath. */
  336. #define path_open()\
  337.   BEGIN\
  338.     if ( !path_is_drawing(ppath) ) {\
  339.       int code_;\
  340.       if ( !path_position_valid(ppath) )\
  341.     return_error(gs_error_nocurrentpoint);\
  342.       code_ = gx_path_new_subpath(ppath);\
  343.       if ( code_ < 0 ) return code_;\
  344.     }\
  345.   END
  346.  
  347. /* Macros for allocating path segments. */
  348. /* Note that they assume that ppath points to the path. */
  349. /* We have to split the macro into two because of limitations */
  350. /* on the size of a single statement (sigh). */
  351. #define path_alloc_segment(pseg,ctype,pstype,stype,snotes,cname)\
  352.   path_unshare(ppath);\
  353.   psub = ppath->current_subpath;\
  354.   if( !(pseg = gs_alloc_struct(ppath->memory, ctype, pstype, cname)) )\
  355.     return_error(gs_error_VMerror);\
  356.   pseg->type = stype, pseg->notes = snotes, pseg->next = 0
  357. #define path_alloc_link(pseg)\
  358.   { segment *prev = psub->last;\
  359.     prev->next = (segment *)pseg;\
  360.     pseg->prev = prev;\
  361.     psub->last = (segment *)pseg;\
  362.   }
  363.  
  364. /* Make a new path (newpath). */
  365. int
  366. gx_path_new(gx_path * ppath)
  367. {
  368.     gx_path_segments *psegs = ppath->segments;
  369.  
  370.     if (gx_path_is_shared(ppath)) {
  371.     int code = path_alloc_segments(&ppath->segments, ppath->memory,
  372.                        "gx_path_new");
  373.  
  374.     if (code < 0)
  375.         return code;
  376.     rc_decrement(psegs, "gx_path_new");
  377.     } else {
  378.     rc_free_path_segments_local(psegs->rc.memory, psegs, "gx_path_new");
  379.     }
  380.     gx_path_init_contents(ppath);
  381.     return 0;
  382. }
  383.  
  384. /* Open a new subpath. */
  385. /* The client must invoke path_update_xxx. */
  386. private int
  387. gx_path_new_subpath(gx_path * ppath)
  388. {
  389.     subpath *psub;
  390.     subpath *spp;
  391.  
  392.     path_alloc_segment(spp, subpath, &st_subpath, s_start, sn_none,
  393.                "gx_path_new_subpath");
  394.     spp->last = (segment *) spp;
  395.     spp->curve_count = 0;
  396.     spp->is_closed = 0;
  397.     spp->pt = ppath->position;
  398.     if (!psub) {        /* first subpath */
  399.     ppath->first_subpath = spp;
  400.     spp->prev = 0;
  401.     } else {
  402.     segment *prev = psub->last;
  403.  
  404.     prev->next = (segment *) spp;
  405.     spp->prev = prev;
  406.     }
  407.     ppath->current_subpath = spp;
  408.     ppath->subpath_count++;
  409.     trace_segment("[P]", (const segment *)spp);
  410.     return 0;
  411. }
  412.  
  413. /* Add a point to the current path (moveto). */
  414. int
  415. gx_path_add_point(gx_path * ppath, fixed x, fixed y)
  416. {
  417.     if (ppath->bbox_set)
  418.     check_in_bbox(ppath, x, y);
  419.     ppath->position.x = x;
  420.     ppath->position.y = y;
  421.     path_update_moveto(ppath);
  422.     return 0;
  423. }
  424.  
  425. /* Add a relative point to the current path (rmoveto). */
  426. int
  427. gx_path_add_relative_point(gx_path * ppath, fixed dx, fixed dy)
  428. {
  429.     if (!path_position_in_range(ppath))
  430.     return_error((path_position_valid(ppath) ? gs_error_limitcheck :
  431.               gs_error_nocurrentpoint));
  432.     {
  433.     fixed nx = ppath->position.x + dx, ny = ppath->position.y + dy;
  434.  
  435.     /* Check for overflow in addition. */
  436.     if (((nx ^ dx) < 0 && (ppath->position.x ^ dx) >= 0) ||
  437.         ((ny ^ dy) < 0 && (ppath->position.y ^ dy) >= 0)
  438.         )
  439.         return_error(gs_error_limitcheck);
  440.     if (ppath->bbox_set)
  441.         check_in_bbox(ppath, nx, ny);
  442.     ppath->position.x = nx;
  443.     ppath->position.y = ny;
  444.     }
  445.     path_update_moveto(ppath);
  446.     return 0;
  447. }
  448.  
  449. /* Set the segment point and the current point in the path. */
  450. /* Assumes ppath points to the path. */
  451. #define path_set_point(pseg, fx, fy)\
  452.     (pseg)->pt.x = ppath->position.x = (fx),\
  453.     (pseg)->pt.y = ppath->position.y = (fy)
  454.  
  455. /* Add a line to the current path (lineto). */
  456. int
  457. gx_path_add_line_notes(gx_path * ppath, fixed x, fixed y, segment_notes notes)
  458. {
  459.     subpath *psub;
  460.     line_segment *lp;
  461.  
  462.     if (ppath->bbox_set)
  463.     check_in_bbox(ppath, x, y);
  464.     path_open();
  465.     path_alloc_segment(lp, line_segment, &st_line, s_line, notes,
  466.                "gx_path_add_line");
  467.     path_alloc_link(lp);
  468.     path_set_point(lp, x, y);
  469.     path_update_draw(ppath);
  470.     trace_segment("[P]", (segment *) lp);
  471.     return 0;
  472. }
  473.  
  474. /* Add multiple lines to the current path. */
  475. /* Note that all lines have the same notes. */
  476. int
  477. gx_path_add_lines_notes(gx_path *ppath, const gs_fixed_point *ppts, int count,
  478.             segment_notes notes)
  479. {
  480.     subpath *psub;
  481.     segment *prev;
  482.     line_segment *lp = 0;
  483.     int i;
  484.     int code = 0;
  485.  
  486.     if (count <= 0)
  487.     return 0;
  488.     path_unshare(ppath);
  489.     path_open();
  490.     psub = ppath->current_subpath;
  491.     prev = psub->last;
  492.     /*
  493.      * We could do better than the following, but this is a start.
  494.      * Note that we don't make any attempt to undo partial additions
  495.      * if we fail partway through; this is equivalent to what would
  496.      * happen with multiple calls on gx_path_add_line.
  497.      */
  498.     for (i = 0; i < count; i++) {
  499.     fixed x = ppts[i].x;
  500.     fixed y = ppts[i].y;
  501.     line_segment *next;
  502.  
  503.     if (ppath->bbox_set && outside_bbox(ppath, x, y)) {
  504.         code = gs_note_error(gs_error_rangecheck);
  505.         break;
  506.     }
  507.     if (!(next = gs_alloc_struct(ppath->memory, line_segment,
  508.                      &st_line, "gx_path_add_lines"))
  509.         ) {
  510.         code = gs_note_error(gs_error_VMerror);
  511.         break;
  512.     }
  513.     lp = next;
  514.     lp->type = s_line;
  515.     lp->notes = notes;
  516.     prev->next = (segment *) lp;
  517.     lp->prev = prev;
  518.     lp->pt.x = x;
  519.     lp->pt.y = y;
  520.     prev = (segment *) lp;
  521.     trace_segment("[P]", (segment *) lp);
  522.     }
  523.     if (lp != 0)
  524.     ppath->position.x = lp->pt.x,
  525.         ppath->position.y = lp->pt.y,
  526.         psub->last = (segment *) lp,
  527.         lp->next = 0,
  528.         path_update_draw(ppath);
  529.     return code;
  530. }
  531.  
  532. /* Add a rectangle to the current path. */
  533. /* This is a special case of adding a closed polygon. */
  534. int
  535. gx_path_add_rectangle(gx_path * ppath, fixed x0, fixed y0, fixed x1, fixed y1)
  536. {
  537.     gs_fixed_point pts[3];
  538.     int code;
  539.  
  540.     pts[0].x = x0;
  541.     pts[1].x = pts[2].x = x1;
  542.     pts[2].y = y0;
  543.     pts[0].y = pts[1].y = y1;
  544.     if ((code = gx_path_add_point(ppath, x0, y0)) < 0 ||
  545.     (code = gx_path_add_lines(ppath, pts, 3)) < 0 ||
  546.     (code = gx_path_close_subpath(ppath)) < 0
  547.     )
  548.     return code;
  549.     return 0;
  550. }
  551.  
  552. /* Add a curve to the current path (curveto). */
  553. int
  554. gx_path_add_curve_notes(gx_path * ppath,
  555.          fixed x1, fixed y1, fixed x2, fixed y2, fixed x3, fixed y3,
  556.             segment_notes notes)
  557. {
  558.     subpath *psub;
  559.     curve_segment *lp;
  560.  
  561.     if (ppath->bbox_set) {
  562.     check_in_bbox(ppath, x1, y1);
  563.     check_in_bbox(ppath, x2, y2);
  564.     check_in_bbox(ppath, x3, y3);
  565.     }
  566.     path_open();
  567.     path_alloc_segment(lp, curve_segment, &st_curve, s_curve, notes,
  568.                "gx_path_add_curve");
  569.     path_alloc_link(lp);
  570.     lp->p1.x = x1;
  571.     lp->p1.y = y1;
  572.     lp->p2.x = x2;
  573.     lp->p2.y = y2;
  574.     path_set_point(lp, x3, y3);
  575.     psub->curve_count++;
  576.     ppath->curve_count++;
  577.     path_update_draw(ppath);
  578.     trace_segment("[P]", (segment *) lp);
  579.     return 0;
  580. }
  581.  
  582. /*
  583.  * Add an approximation of an arc to the current path.
  584.  * The current point of the path is the initial point of the arc;
  585.  * parameters are the final point of the arc
  586.  * and the point at which the extended tangents meet.
  587.  * We require that the arc be less than a semicircle.
  588.  * The arc may go either clockwise or counterclockwise.
  589.  * The approximation is a very simple one: a single curve
  590.  * whose other two control points are a fraction F of the way
  591.  * to the intersection of the tangents, where
  592.  *      F = (4/3)(1 / (1 + sqrt(1+(d/r)^2)))
  593.  * where r is the radius and d is the distance from either tangent
  594.  * point to the intersection of the tangents.  This produces
  595.  * a curve whose center point, as well as its ends, lies on
  596.  * the desired arc.
  597.  *
  598.  * Because F has to be computed in user space, we let the client
  599.  * compute it and pass it in as an argument.
  600.  */
  601. int
  602. gx_path_add_partial_arc_notes(gx_path * ppath,
  603. fixed x3, fixed y3, fixed xt, fixed yt, floatp fraction, segment_notes notes)
  604. {
  605.     fixed x0 = ppath->position.x, y0 = ppath->position.y;
  606.  
  607.     return gx_path_add_curve_notes(ppath,
  608.                    x0 + (fixed) ((xt - x0) * fraction),
  609.                    y0 + (fixed) ((yt - y0) * fraction),
  610.                    x3 + (fixed) ((xt - x3) * fraction),
  611.                    y3 + (fixed) ((yt - y3) * fraction),
  612.                    x3, y3, notes | sn_from_arc);
  613. }
  614.  
  615. /* Append a path to another path, and reset the first path. */
  616. /* Currently this is only used to append a path to its parent */
  617. /* (the path in the previous graphics context). */
  618. int
  619. gx_path_add_path(gx_path * ppath, gx_path * ppfrom)
  620. {
  621.     path_unshare(ppfrom);
  622.     path_unshare(ppath);
  623.     if (ppfrom->first_subpath) {    /* i.e. ppfrom not empty */
  624.     if (ppath->first_subpath) {    /* i.e. ppath not empty */
  625.         subpath *psub = ppath->current_subpath;
  626.         segment *pseg = psub->last;
  627.         subpath *pfsub = ppfrom->first_subpath;
  628.  
  629.         pseg->next = (segment *) pfsub;
  630.         pfsub->prev = pseg;
  631.     } else
  632.         ppath->first_subpath = ppfrom->first_subpath;
  633.     ppath->current_subpath = ppfrom->current_subpath;
  634.     ppath->subpath_count += ppfrom->subpath_count;
  635.     ppath->curve_count += ppfrom->curve_count;
  636.     }
  637.     /* Transfer the remaining state. */
  638.     ppath->position = ppfrom->position;
  639.     ppath->outside_position = ppfrom->outside_position;
  640.     ppath->state_flags = ppfrom->state_flags;
  641.     /* Reset the source path. */
  642.     gx_path_init_contents(ppfrom);
  643.     return 0;
  644. }
  645.  
  646. /* Add a path or its bounding box to the enclosing path, */
  647. /* and reset the first path.  Only used for implementing charpath and its */
  648. /* relatives. */
  649. int
  650. gx_path_add_char_path(gx_path * to_path, gx_path * from_path,
  651.               gs_char_path_mode mode)
  652. {
  653.     int code;
  654.     gs_fixed_rect bbox;
  655.  
  656.     switch (mode) {
  657.     default:        /* shouldn't happen! */
  658.         gx_path_new(from_path);
  659.         return 0;
  660.     case cpm_charwidth: {
  661.         gs_fixed_point cpt;
  662.  
  663.         code = gx_path_current_point(from_path, &cpt);
  664.         if (code < 0)
  665.         break;
  666.         return gx_path_add_point(to_path, cpt.x, cpt.y);
  667.     }
  668.     case cpm_true_charpath:
  669.     case cpm_false_charpath:
  670.         return gx_path_add_path(to_path, from_path);
  671.     case cpm_true_charboxpath:
  672.         gx_path_bbox(from_path, &bbox);
  673.         code = gx_path_add_rectangle(to_path, bbox.p.x, bbox.p.y,
  674.                      bbox.q.x, bbox.q.y);
  675.         break;
  676.     case cpm_false_charboxpath:
  677.         gx_path_bbox(from_path, &bbox);
  678.         code = gx_path_add_point(to_path, bbox.p.x, bbox.p.y);
  679.         if (code >= 0)
  680.         code = gx_path_add_line(to_path, bbox.q.x, bbox.q.y);
  681.         break;
  682.     }
  683.     if (code < 0)
  684.     return code;
  685.     gx_path_new(from_path);
  686.     return 0;
  687. }
  688.  
  689. /* Close the current subpath. */
  690. int
  691. gx_path_close_subpath_notes(gx_path * ppath, segment_notes notes)
  692. {
  693.     subpath *psub;
  694.     line_close_segment *lp;
  695.     int code;
  696.  
  697.     if (!path_subpath_open(ppath))
  698.     return 0;
  699.     if (path_last_is_moveto(ppath)) {
  700.     /* The last operation was a moveto: create a subpath. */
  701.     code = gx_path_new_subpath(ppath);
  702.     if (code < 0)
  703.         return code;
  704.     }
  705.     path_alloc_segment(lp, line_close_segment, &st_line_close,
  706.                s_line_close, notes, "gx_path_close_subpath");
  707.     path_alloc_link(lp);
  708.     path_set_point(lp, psub->pt.x, psub->pt.y);
  709.     lp->sub = psub;
  710.     psub->is_closed = 1;
  711.     path_update_closepath(ppath);
  712.     trace_segment("[P]", (segment *) lp);
  713.     return 0;
  714. }
  715.  
  716. /* Remove the last line from the current subpath, and then close it. */
  717. /* The Type 1 font hinting routines use this if a path ends with */
  718. /* a line to the start followed by a closepath. */
  719. int
  720. gx_path_pop_close_notes(gx_path * ppath, segment_notes notes)
  721. {
  722.     subpath *psub = ppath->current_subpath;
  723.     segment *pseg;
  724.     segment *prev;
  725.  
  726.     if (psub == 0 || (pseg = psub->last) == 0 ||
  727.     pseg->type != s_line
  728.     )
  729.     return_error(gs_error_unknownerror);
  730.     prev = pseg->prev;
  731.     prev->next = 0;
  732.     psub->last = prev;
  733.     gs_free_object(ppath->memory, pseg, "gx_path_pop_close_subpath");
  734.     return gx_path_close_subpath_notes(ppath, notes);
  735. }
  736.  
  737. /* ------ Internal routines ------ */
  738.  
  739. /*
  740.  * Copy the current path, because it was shared.
  741.  */
  742. private int
  743. path_alloc_copy(gx_path * ppath)
  744. {
  745.     gx_path path_new;
  746.     int code;
  747.  
  748.     gx_path_init_local(&path_new, ppath->memory);
  749.     code = gx_path_copy(ppath, &path_new);
  750.     if (code < 0) {
  751.     gx_path_free(&path_new, "path_alloc_copy error");
  752.     return code;
  753.     }
  754.     return gx_path_assign_free(ppath, &path_new);
  755. }
  756.  
  757. /* ------ Debugging printout ------ */
  758.  
  759. #ifdef DEBUG
  760.  
  761. /* Print out a path with a label */
  762. void
  763. gx_dump_path(const gx_path * ppath, const char *tag)
  764. {
  765.     dlprintf2("[P]Path 0x%lx %s:\n", (ulong) ppath, tag);
  766.     gx_path_print(ppath);
  767. }
  768.  
  769. /* Print a path */
  770. void
  771. gx_path_print(const gx_path * ppath)
  772. {
  773.     const segment *pseg = (const segment *)ppath->first_subpath;
  774.  
  775.     dlprintf5("   state_flags=%d subpaths=%d, curves=%d, point=(%f,%f)\n",
  776.           ppath->state_flags, ppath->subpath_count, ppath->curve_count,
  777.           fixed2float(ppath->position.x),
  778.           fixed2float(ppath->position.y));
  779.     dlprintf5("   box=(%f,%f),(%f,%f) last=0x%lx\n",
  780.           fixed2float(ppath->bbox.p.x), fixed2float(ppath->bbox.p.y),
  781.           fixed2float(ppath->bbox.q.x), fixed2float(ppath->bbox.q.y),
  782.           (ulong) ppath->box_last);
  783.     dlprintf4("   segments=0x%lx (refct=%ld, first=0x%lx, current=0x%lx)\n",
  784.           (ulong) ppath->segments, (long)ppath->segments->rc.ref_count,
  785.           (ulong) ppath->segments->contents.subpath_first,
  786.           (ulong) ppath->segments->contents.subpath_current);
  787.     while (pseg) {
  788.     dlputs("");
  789.     gx_print_segment(pseg);
  790.     pseg = pseg->next;
  791.     }
  792. }
  793. private void
  794. gx_print_segment(const segment * pseg)
  795. {
  796.     double px = fixed2float(pseg->pt.x);
  797.     double py = fixed2float(pseg->pt.y);
  798.     char out[80];
  799.  
  800.     sprintf(out, "   0x%lx<0x%lx,0x%lx>:%u",
  801.      (ulong) pseg, (ulong) pseg->prev, (ulong) pseg->next, pseg->notes);
  802.     switch (pseg->type) {
  803.     case s_start:{
  804.         const subpath *const psub = (const subpath *)pseg;
  805.  
  806.         dprintf5("%s: %1.4f %1.4f moveto\t%% #curves=%d last=0x%lx\n",
  807.              out, px, py, psub->curve_count, (ulong) psub->last);
  808.         break;
  809.         }
  810.     case s_curve:{
  811.         const curve_segment *const pcur = (const curve_segment *)pseg;
  812.  
  813.         dprintf7("%s: %1.4f %1.4f %1.4f %1.4f %1.4f %1.4f curveto\n",
  814.               out, fixed2float(pcur->p1.x), fixed2float(pcur->p1.y),
  815.           fixed2float(pcur->p2.x), fixed2float(pcur->p2.y), px, py);
  816.         break;
  817.         }
  818.     case s_line:
  819.         dprintf3("%s: %1.4f %1.4f lineto\n", out, px, py);
  820.         break;
  821.     case s_line_close:{
  822.         const line_close_segment *const plc =
  823.         (const line_close_segment *)pseg;
  824.  
  825.         dprintf4("%s: closepath\t%% %1.4f %1.4f 0x%lx\n",
  826.              out, px, py, (ulong) (plc->sub));
  827.         break;
  828.         }
  829.     default:
  830.         dprintf4("%s: %1.4f %1.4f <type 0x%x>\n", out, px, py, pseg->type);
  831.     }
  832. }
  833.  
  834. #endif /* DEBUG */
  835.